//
//  CatDetailViewController.m
//  代号：47
//
//  Created by 杨佩璋 on 15/7/9.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "CatDetailViewController.h"
#import <UIImageView+WebCache.h>


@interface CatDetailViewController ()
@property (nonatomic,retain) UILabel  *titleLabel;
@property (nonatomic,retain) UILabel *contentLabel;
@property (nonatomic,retain)AFHTTPRequestOperation *operation; // 操作。
@property (nonatomic,retain) NSOperationQueue *queue;  // 操作队列

@property (nonatomic,retain) NSString *titleStr;
@property (nonatomic,retain) NSString *contentStr;
@property (nonatomic,retain) NSArray *imagesArr;


// 三张图片
@property (nonatomic,retain) UIImageView *pic1;
@property (nonatomic,retain) UIImageView *pic2;
@property (nonatomic,retain) UIImageView *pic3;


//tableView
@property (nonatomic,retain) UITableView *mytableView;
//表头表尾
@property (nonatomic,retain) UIView *headView;
@property (nonatomic,retain) UIView *footView;



@end

@implementation CatDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createBackButton];
    [self createTableView];
    [self createTitle];
    [self createContent];
    [self requestData];
    
    
}


// 创建返回按钮

- (void)createBackButton{
    UIButton *button  = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 30, 30, 30);
    [button setBackgroundImage:[UIImage imageNamed:@"iconfont-jiantouzuo(1).png"] forState:UIControlStateNormal];
    [self.view addSubview:button];
    [button addTarget:self action:@selector(mydismiss) forControlEvents:UIControlEventTouchUpInside];
}

- (void)mydismiss{
    [self dismissViewControllerAnimated:YES completion:nil];
}



// 创建标题
- (void)createTitle{
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 20, kScreen_width -120, 50)];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    self.titleLabel.textColor = [UIColor orangeColor];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.numberOfLines = 0;
    [_titleLabel release];
    [self.view addSubview:self.titleLabel];
}





// 创建内容  高度自定义
- (void)createContent{
    _contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, kScreen_width - 20, 100)];
    _contentLabel.font = [UIFont systemFontOfSize:12];
    _contentLabel.numberOfLines = 0;
}





// 创建tableView
- (void)createTableView{
    _mytableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, kScreen_width, kScreen_height - 64) style:UITableViewStylePlain];
    
}



// 设置表头
- (void)setHeaderView{
    _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 20 + self.contentLabel.height +10)];
    [_headView addSubview:_contentLabel];
    _mytableView.tableHeaderView = _headView;
    [_headView release];
    
}


- (void)setFootView{
    
    if (self.imagesArr.count>2) {
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, _pic1.height + 10 +_pic2.height +10+_pic3.height + 10)];
        [_footView addSubview:_pic1];
        [_footView addSubview:_pic2];
        [_footView addSubview:_pic3];
        _mytableView.tableFooterView = _footView;
        [_footView release];
    }else if (self.imagesArr.count>1){
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, _pic1.height + 10 +_pic2.height +10)];
        [_footView addSubview:_pic1];
        [_footView addSubview:_pic2];
        _mytableView.tableFooterView = _footView;
        [_footView release];
    }else if (self.imagesArr.count>0){
        _footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, _pic1.height + 10 )];
        [_footView addSubview:_pic1];
        _mytableView.tableFooterView = _footView;
        [_footView release];
    }
    
}




//请求数据
// AFN 数据请求
- (void)requestData
{
    NSString *str = [NSString stringWithFormat:@"http://api.maoyan.com/sns/topic/%@.json?",self.number];
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    self.operation = operation;
    [operation release];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *html = operation.responseString;
        NSData* data=[html dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSDictionary *dic1 = dic[@"data"];
        
        // 标题
        self.titleStr = dic1[@"title"];
        self.titleLabel.text = self.titleStr;
        
        
        //内容
        self.contentStr = dic1[@"text"];
        self.contentLabel.text = self.contentStr;
        self.contentLabel.height = [self heightForStr:self.contentStr];
        [self setHeaderView];
 
        // 图片数组
        NSArray *arr = dic1[@"images"];
        self.imagesArr = arr;
        [self createPictures];
        [self setFootView];
        
        //设置contentSize
        self.mytableView.contentSize = CGSizeMake(kScreen_width, _headView.height + _footView.height);
        [self.view addSubview:_mytableView];
        
        
    } failure:nil];
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue addOperation:operation];
    self.queue = queue;
    [queue release];
}



// 创建图片
-(void)createPictures{
    
    
    if (self.imagesArr.count>0) {
        CGFloat allWidth = kScreen_width - 20;
        
        // 图片1
        NSDictionary *dic1 = self.imagesArr[0];
        NSString *url1 =  dic1[@"url"];
        CGFloat width1  = [dic1[@"width"] floatValue];
        CGFloat height1 = [dic1[@"height"] floatValue];
        CGFloat myheight1 = allWidth *height1 /width1;
        _pic1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, allWidth, myheight1)];
        [_pic1 sd_setImageWithURL:[NSURL URLWithString:url1]];
        
        if (self.imagesArr.count>1) {
            // 图片2
            NSDictionary *dic2 = self.imagesArr[1];
            NSString *url2 =  dic2[@"url"];
            CGFloat width2  = [dic2[@"width"] floatValue];
            CGFloat height2 = [dic2[@"height"] floatValue];
            CGFloat y2 = _pic1.y + _pic1.height + 10;
            CGFloat myheight2 = allWidth *height2 /width2;
            _pic2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, y2, allWidth, myheight2)];
            [_pic2 sd_setImageWithURL:[NSURL URLWithString:url2]];

            // 图片3
            if (self.imagesArr.count>2) {
                NSDictionary *dic3 = self.imagesArr[2];
                NSString *url3 =  dic3[@"url"];
                CGFloat width3  = [dic3[@"width"] floatValue];
                CGFloat height3 = [dic3[@"height"] floatValue];
                CGFloat y3 = _pic2.y + _pic2.height + 10;
                CGFloat myheight3 = allWidth *height3 /width3;
                _pic3 = [[UIImageView alloc] initWithFrame:CGRectMake(10, y3, allWidth, myheight3)];
                [_pic3 sd_setImageWithURL:[NSURL URLWithString:url3]];
            }
        }
        
    
    }
    
    
    
    
}


- (CGFloat)heightForStr:(NSString *)str{
    CGRect rect = [str boundingRectWithSize:CGSizeMake(kScreen_width - 20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil];
    return rect.size.height;
}







-(void)dealloc{
    [_number release];
    [_pic3 release];
    [_pic2 release];
    [_pic1 release];
    [_mytableView release];
    [_imagesArr release];
    [_operation release];
    [_queue release];
    [_titleStr release];
    [_contentStr release];
    [_titleLabel release];
    [_contentLabel release];
    [super dealloc];
}

@end
